﻿using RBAC.LuFang.Domain;
using RBAC.LuFang.Domain.DTO;
using RBAC.LuFang.Domain.Traceability;
using RBAC.LuFang.Repository.lmpl;
using RBAC.LuFang.Repository.lnterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.LuFang.Domin.Services.TraceabilityService
{
    public class slaughterService: IslaughterService
    {
        private readonly IslaughterRepository _slaRepository;
        private readonly IimgesRepository _imgRepository;
        private readonly IEnterpriseRepository _enRepository;
        private readonly ICountyRepository _countRepository;//县
        private readonly ITheCityRepository _thecityRepository;//市
        private readonly IProvincesRepository _provRepository;//省

        public slaughterService(IslaughterRepository slaRepository, IimgesRepository imgRepository, IEnterpriseRepository enRepository, ICountyRepository countRepository, ITheCityRepository thecityRepository, IProvincesRepository provRepository)
        {
            _slaRepository = slaRepository;
            _imgRepository = imgRepository;
            _enRepository = enRepository;
            _countRepository = countRepository;
            _thecityRepository = thecityRepository;
            _provRepository = provRepository;
        }





        /// <summary>
        /// 反填屠宰信息
        /// </summary>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<slaughter> Fanslaughter(int goodsid)
        {
            try
            {
                var res=(await _slaRepository.GetAllAsync()).Where(x=>x.goodsId==goodsid).FirstOrDefault();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }

       

        /// <summary>
        /// 显示屠宰信息
        /// </summary>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<List<slaughter>> Getsla(int goodsid)
        {
            try
            {
                return await _slaRepository.Get(x => x.goodsId == goodsid);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 更新屠宰信息
        /// </summary>
        /// <param name="slaughter"></param>
        /// <returns></returns>
        public async Task<int> Updslaughter(slaughter slaughter)
        {
            try
            {
                return await _slaRepository.GetUpd(slaughter);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示相关资质
        /// </summary>
        /// <param name="imgtype"></param>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<List<imges>> GetRelatedPic(string imgtype, int goodsid)
        {
            try
            {
                var res = await _imgRepository.Get(x => x.imgestype == imgtype && x.goodsId == goodsid);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除相关资质
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelRelatedPid(int id)
        {
            try
            {
                var res = await _imgRepository.GetByidAsync(id);
                return await _imgRepository.Del(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示屠宰下的企业信息
        /// </summary>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<List<GetslaEnterpriseDTO>> GetslaEnterprise(int goodsid)
        {
            try
            {
                var res = (from en in await _enRepository.GetAllAsync()
                           join pr in await _provRepository.GetAllAsync()
                          on en.provinceID equals pr.provinceID
                          join th in await _thecityRepository.GetAllAsync()
                          on en.theCityID equals th.theCityID
                          join c in await _countRepository.GetAllAsync()
                          on en.countyID equals c.countyID
                          where en.goodsId == goodsid
                          select new GetslaEnterpriseDTO
                          {
                              enterpriseid=en.enterpriseid,
                              livestockfarm=en.livestockfarm,
                              personincharge=en.personincharge,
                              number=en.number,
                              enterprisename= en.enterprisename,
                              provinceID= en.provinceID,
                              theCityID=en.theCityID,
                              countyID=en.countyID,
                              enterpriseDate=en.enterpriseDate,
                              enterpriseDesc= en.enterpriseDesc,
                              enterpriseBan= en.enterpriseBan,
                              enterprisePeople=en.enterprisePeople,
                              goodsId=en.goodsId,
                              provinceName=pr.provinceName,
                              theCityName=th.theCityName,
                              countyName=c.countyName
                          }).ToList();

                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反填屠宰下的企业信息
        /// </summary>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<Enterprise> FanslaEnterprise(int goodsid)
        {
            try
            {
                var res = (await _enRepository.GetAllAsync()).Where(x => x.goodsId == goodsid).FirstOrDefault();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 更新屠宰下的企业信息
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        public async Task<int> updslaEnterprise(Enterprise enterprise)
        {
            try
            {
                return await _enRepository.GetUpd(enterprise);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示屠宰下的企业照片
        /// </summary>
        /// <param name="imgtype"></param>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        public async Task<List<imges>> GetslaEnterPic(string imgtype, int goodsid)
        {
            try
            {
                return await _imgRepository.Get(x => x.imgestype == imgtype && x.goodsId == goodsid);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除屠宰下的企业照片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelslaEnterPic(int id)
        {
            try
            {
                var res = await _imgRepository.GetByidAsync(id);
                return await _imgRepository.Del(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
